home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 25 / CU Amiga Magazine's Super CD-ROM 25 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-08].iso / CUCD / Utilities / Type1Manager / src / amishmalloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-05-16  |  2.5 KB  |  174 lines

  1. /**
  2.  * INCLUDES
  3.  **/
  4. #include <proto/exec.h>
  5. #include <proto/dos.h>
  6. #include <exec/memory.h>
  7.  
  8. #ifdef MEMTRACE
  9. #include <stdio.h>
  10. #endif
  11.  
  12. #include <string.h>
  13. //#include <pools_protos.h>
  14.  
  15.  
  16. /**
  17.  * GLOBALS
  18.  **/
  19. extern void *AmishPool;
  20. extern void *AmishPool2;
  21. extern struct ExecBase *SysBase;
  22.  
  23.  
  24. /**
  25.  * LOCAL PROTOTYPES
  26.  **/
  27. static void *__inline MyAllocVec(void *pool, ULONG size);
  28. static void __inline MyFreeVec(void *pool, void *memory);
  29.  
  30.  
  31. /**
  32.  * addmemory
  33.  **/
  34. void addmemory(long size)
  35. {
  36.     AmishPool = CreatePool(MEMF_CLEAR, size, size);
  37. }
  38.  
  39.  
  40. /**
  41.  * delmemory
  42.  **/
  43. void delmemory(void)
  44. {
  45.     if (AmishPool)
  46.         DeletePool(AmishPool);
  47.     AmishPool = NULL;
  48. }
  49.  
  50.  
  51. /**
  52.  * xiMalloc
  53.  **/
  54. char *xiMalloc(unsigned int Size)
  55. {
  56.     return (char *)MyAllocVec(AmishPool, Size);
  57. }
  58.  
  59.  
  60. /**
  61.  * xiFree
  62.  **/
  63. void xiFree(void *addr)
  64. {
  65.     if (addr)
  66.         MyFreeVec(AmishPool, addr);
  67.     return;
  68. }
  69.  
  70.  
  71. static void *__inline MyAllocVec(void *pool, ULONG size)
  72. {
  73.     UBYTE *memory;
  74.  
  75.     size += sizeof(ULONG);
  76.  
  77.     memory = AllocPooled(pool, size);
  78.  
  79.     if (!memory)
  80.         return NULL;
  81.  
  82.     *((ULONG *) memory) = size;
  83.  
  84.     return (void *)(memory + sizeof(ULONG));
  85. }
  86.  
  87.  
  88. static void __inline MyFreeVec(void *pool, void *memory)
  89. {
  90.     void *realMemory;
  91.     ULONG size;
  92.  
  93.     if (!memory)
  94.         return;
  95.  
  96.     realMemory = (UBYTE *) memory - sizeof(ULONG);
  97.     size = *((ULONG *) realMemory);
  98.  
  99.     FreePooled(pool, realMemory, size);
  100. }
  101.  
  102. #ifdef MEMTRACE
  103. FILE *tempfp;
  104. #endif
  105.  
  106. /**
  107.  * addmemory2
  108.  **/
  109. void addmemory2(long size)
  110. {
  111. #ifdef MEMTRACE
  112.     tempfp = fopen("ram:memtrace", "w");
  113. #endif
  114.     AmishPool2 = CreatePool(MEMF_CLEAR, size, size);
  115. }
  116.  
  117.  
  118. /**
  119.  * delmemory2
  120.  **/
  121. void delmemory2(void)
  122. {
  123.     if (AmishPool2)
  124.         DeletePool(AmishPool2);
  125.     AmishPool2 = NULL;
  126. #ifdef MEMTRACE
  127.     fclose(tempfp);
  128. #endif
  129. }
  130.  
  131. #ifdef MEMTRACE
  132. char *Xalloc(int size, char *file, char *func, int linenum)
  133. #else
  134. char *Xalloc(int size)
  135. #endif
  136. {
  137. #ifdef MEMTRACE
  138.     unsigned long *tmp;
  139.     static unsigned long idnum = 0;
  140.  
  141.     tmp = (unsigned long *)MyAllocVec(AmishPool2, size+sizeof(unsigned long));
  142.     tmp[0] = idnum++;
  143.     fprintf(tempfp, "%4d calloc   %15s%5d\t%16s\t%lx\t%d\n", tmp[0], file, linenum, func, (unsigned int)tmp+1, size);
  144.  
  145.     return (char *)(tmp + 1);
  146. #else
  147.     return (char *)MyAllocVec(AmishPool2, size);
  148. #endif
  149. }
  150.  
  151.  
  152. #ifdef MEMTRACE
  153. void Xfree(char *p, char *file, char *func, int linenum)
  154. #else
  155. void Xfree(char *p)
  156. #endif
  157. {
  158. #ifdef MEMTRACE
  159.     unsigned long *tmp = NULL;
  160.  
  161.     if (!p)
  162.         return;
  163.  
  164.     tmp = ((unsigned long *)p) - 1;
  165.     fprintf(tempfp, "%4d free     %15s%5d\t%16s\t%lx\n", tmp[0], file, linenum, func, (unsigned int)tmp+1);
  166.     MyFreeVec(AmishPool2, (void *)tmp);
  167. #else
  168.     MyFreeVec(AmishPool2, (void *)p);
  169. #endif
  170.     return;
  171. }
  172.  
  173.  
  174.